package com.tony.demo.ui.widgets;

import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.puhui.lib.utils.DMLog;
import com.tony.demo.R;
import com.tony.demo.mvp.presenter.impl.BasePresenter;
import com.tony.demo.ui.base.BaseActivity;

/**
 * Copyright:
 * Created by TangJian on 2019/4/18.
 * Description:
 * Modified:
 */

public class ScanningActivity extends BaseActivity {
    TextView titleText;
    ImageView imageOne;
    ImageView imageTwo;
    ImageView imageThree;
    ImageView imageFour;
    ImageView musicImage;

    private Animation animationOne, animationTwo, animationThree, animationFour;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int getViewLayout() {
        return R.layout.activity_scanning;
    }

    protected void initView() {
        titleText = findViewById(R.id.title_text);
        imageOne = findViewById(R.id.image_one);
        imageTwo = findViewById(R.id.image_two);
        imageThree = findViewById(R.id.image_three);
        imageFour = findViewById(R.id.image_four);
        musicImage = findViewById(R.id.music_image);

        titleText.setText("扫描测试");

        handlerTest();

        testMemoryMonitor();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                if (animationOne != null) {
                    animationOne.cancel();
                    animationTwo.cancel();
                    animationThree.cancel();
                    animationFour.cancel();
                }
                finish();
                break;
            case R.id.music_image:
                startScan();
                break;
        }
    }

    private void testMemoryMonitor() {
        for (int i = 0; i < 50; i++) {
            Bitmap bitmap = imageOne.getDrawingCache();
        }
    }

    //    @SuppressWarnings("HandlerLeak")  //如果使用了handler.callBack就不会有这个警告
    private void handlerTest() {
        new Thread(new Runnable() {
            @Override
            public void run() {
//                Handler handler = new Handler() {
//                    @Override
//                    public void handleMessage(Message msg) {
//                        super.handleMessage(msg);
//                    }
//                };


                //java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
                //没有调用该方法，会出现上面的异常；而且该方法必须在Handler创建之前调用，否则也报上面的异常
                Looper.prepare();
                //解释：
                //该方法会通过ThreadLocal<Looper>.set方法创建一个新的Looper给当前线程使用
                //每个线程都应该有自己的Looper。在使用Handler的时候需要通过Looper来不断的从
                //消息队列中轮询消息并返回给Handler处理。而Looper用来维护MessageQueue。
                //在new Handler的时候，会通过Looper.myLooper()方法来获取当前线程的Looper，如果没有提前prepare的话，
                //那么得到的结果就是null，然后抛出上面的异常
                //ThreadLocal可以为每个线程保存一个本地变量，这个变量不会受到其他线程的干扰

                Handler handler = new Handler(new Handler.Callback() {
                    @Override
                    public boolean handleMessage(Message msg) {
                        doHandleMessage();
                        DMLog.e(this.getClass().getSimpleName(), "handleMessage  handleMessage");
                        return false;
                    }
                });

                handler.sendEmptyMessage(0);

                // 不调用Looper.loop()该方法，handler获取不到消息；
                // 该方法在handler.sendEmptyMessage()之前调用，handler也获取不到消息
                Looper.loop();

                //loop()后面的代码不会被执行?????
                DMLog.e(this.getClass().getSimpleName(), "current thread is " + Thread.currentThread());
            }
        }).start();
    }

    private void doHandleMessage() {
        SystemClock.sleep(200);
    }

    private void startScan() {
        if (animationOne == null) {
            animationOne = AnimationUtils.loadAnimation(this, R.anim.scanning_animation);
            animationTwo = AnimationUtils.loadAnimation(this, R.anim.scanning_animation);
            animationThree = AnimationUtils.loadAnimation(this, R.anim.scanning_animation);
            animationFour = AnimationUtils.loadAnimation(this, R.anim.scanning_animation);

            animationTwo.setStartOffset(200);
            animationThree.setStartOffset(400);
            animationFour.setStartOffset(600);
        }

        animationOne.cancel();
        animationTwo.cancel();
        animationThree.cancel();
        animationFour.cancel();

        imageOne.startAnimation(animationOne);
        imageTwo.startAnimation(animationTwo);
        imageThree.startAnimation(animationThree);
        imageFour.startAnimation(animationFour);
    }
}
